<?php
//Knowledge Management Controller By Teerapong Singthong.
class Organization_model extends Model{
	function __construct(){
		parent::Model();
	}
	
	function setdata(){
		$data["title"] = "Organization.";	//set title.
		$data["topic"] = "Organization - Knowledge Management.";
		return $data;	//return $data -> main funct.
	}
	
	function add_organization_news()
	{
		$c_org_title = $this->input->post('c_org_title');
		$org_id = $this->input->post('org_id');
		
		//$content_2 = $this->input->post('content_2');
		if ( isset( $_POST ) )
			$postArray = &$_POST ;			// 4.1.0 or later, use $_POST
		else
			$postArray = &$HTTP_POST_VARS ;	// prior to 4.1.0, use HTTP_POST_VARS
		
		foreach ( $postArray as $sForm => $value )
		{
			if ( get_magic_quotes_gpc() )
				$postedValue = htmlspecialchars( stripslashes( $value ) ) ;
			else
			$postedValue = htmlspecialchars( $value ) ;
			//echo $sForm;
			$postedValue;
		}
		
		$date_format = "%Y-%m-%d %h:%i:%a";
		$dated = mdate($date_format);
		$member_id = $this->session->userdata('member_id');
		
		$data = array(
			'c_org_title' => $c_org_title,
			'c_org_content' => $postedValue,
			'c_org_date' => $dated,
			'member_id' => $member_id,
			'org_id' => $org_id,
		);
		
		$insert = $this->db->insert('content_of_organization',$data);
		return $insert;
	}
	
	function view_details_by_id(){
	
	}
	
	function delete_org_news_by_id($id){
		$this->db->where('c_org_id',$id);
		$this->db->delete('content_of_organization');
		return true;
	}
	
	function edit_org_news_by_id($id){
		$c_org_title = $this->input->post('c_org_title');
		$c_org_content = $this->input->post('content_2');
		$org_id = $this->input->post('org_id');
		$date_format = "%Y-%m-%d %h:%i:%a";
		$dated = mdate($date_format);
		$member_id = $this->session->userdata('member_id');
		
		$data = array(
			'c_org_title' => $c_org_title,
			'c_org_content' => $c_org_content,
			'c_org_date' => $dated,
			'org_id' => $org_id,
			'member_id' => $member_id,
		);
		
		$this->db->where('c_org_id',$id);
		$this->db->update('content_of_organization',$data);
		
		return true;
	}
	
	function assign_staff($data){
		$staff_id = $data["staff_id"];
		$org_id = $data["org_id"];
		
		//check qnique
		$this->db->where('member_id',$staff_id);
		$qnique = $this->db->get('staff_of_organization');
		if($qnique->num_rows == 1)
		{
			return false;
		}
		else
		{
		
		/*insert to staff_of_organization*/
		$insert_staff = array(
			'member_id' => $staff_id,
			'org_id' => $org_id,
		);
		$this->db->where('org_id',$org_id);
		$this->db->update('staff_of_organization',$insert_staff);
		
		/*update to organization*/
		$update_org_staff = array(
			'staff_id' => $staff_id,
		);
		$this->db->where('org_id',$org_id);
		$this->db->update('organization',$update_org_staff);
		
		return true;
		}
	}
	
	function edit_organization(){
		$org_name = $this->input->post('e_org_name');
		$org_id = $this->input->post('e_org_id');
		$org_description = $this->input->post('e_org_desc');
		
		$data = array(
			'org_name' => $org_name,
			'org_description' => $org_description
		);
		
		$this->db->where('org_id',$org_id);
		$query = $this->db->update('organization',$data);
		
		return true;
	}
	
	function add_organization(){
		$org_name = $this->input->post('org_name');
		$org_description = $this->input->post('org_desc');
		
		$data = array(
			'org_name' => $org_name,
			'org_description' => $org_description
		);
		
		$this->db->insert('organization',$data);
		
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
}